package com.heima.search.service;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.dtos.HistorySearchDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.search.pojos.ApUserSearch;
import com.heima.search.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class ApUserSearchServiceImpl implements ApUserSearchService {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    @Async
    public void insert(String keyword, Integer userId) {

        //查询当前用户的搜索词关键词
        Query query = Query.query(Criteria.where("userId").is(userId).and("keyword").is(keyword));
        ApUserSearch apUserSearch = mongoTemplate.findOne(query, ApUserSearch.class);

        //存在 更新创建时间
        if (apUserSearch != null) {
            apUserSearch.setCreatedTime(new Date());
            mongoTemplate.save(apUserSearch);
            return;
        }

        //不存在 判断当前历史记录总量是否超过10
        ApUserSearch apUserSearch1 = new ApUserSearch();
        apUserSearch1.setUserId(userId);
        apUserSearch1.setKeyword(keyword);
        apUserSearch1.setCreatedTime(new Date());

//        Query query1 = Query.query(Criteria.where("userId").is(userId));
//        query1.with(Sort.by(Sort.Direction.DESC, "createdTime"));
        mongoTemplate.save(apUserSearch1);



    }

    @Override
    public ResponseResult findUserSearch() {

        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            log.warn("用户没有登录");
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        Integer id = user.getId();

        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(id));
        query.with(Sort.by("createdTime").descending());
        query.limit(10);
        List<ApUserSearch> list = mongoTemplate.find(query, ApUserSearch.class);

        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult delUserSearch(HistorySearchDto dto) {

        if (dto == null || dto.getId() == null) {
            log.warn("参数为空");
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            log.warn("用户没有登录");
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        Integer id = user.getId();
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(id).and("id").is(dto.getId()));

        mongoTemplate.remove(query,ApUserSearch.class);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
